package cn.ffcs.uec.salary.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.salary.action.bean.UecSalaryPrjEditBean;
import cn.ffcs.uec.salary.constant.EnumUecSalary;
import cn.ffcs.uec.salary.manager.UecSalaryProjectManager;
import cn.ffcs.uec.salary.model.UecSalaryProject;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecSalaryPrjEditComposer extends BasePortletComposer {
	private static final long serialVersionUID = 1233542378870873152L;

	private UecSalaryPrjEditBean bean = new UecSalaryPrjEditBean();

	private String opType;

	@Resource
	private UecSalaryProjectManager uecSalaryProjectManager;

	UecSalaryProject uecSalaryProject;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * window初始化.
	 * 
	 * @throws Exception
	 *             异常
	 */
	public void onCreate$uecSalaryPrjEditWin() throws Exception {
		bindBean();
	}

	/**
	 * 页面初始化
	 * 
	 * @throws Exception
	 */
	public void bindBean() throws Exception {
		opType = StrUtil.strnull(arg.get("opType"));
		if (EnumUecSalary.ADD.getValue().equals(opType)) {
			bean.getUecSalaryPrjEditWin().setTitle("薪资项目新增");
		} else if (EnumUecSalary.MOD.getValue().equals(opType)) {
			bean.getUecSalaryPrjEditWin().setTitle("薪资项目修改");
			initPageBean();
		} else {
			bean.getUecSalaryPrjEditWin().setTitle("薪资项目查看");
			bean.getBtnToolBar().setVisible(false);
			PubUtil.disableComponent(bean.getUecSalaryPrjDiv(), true);
			initPageBean();
		}
	}

	public void initPageBean() throws Exception {
		uecSalaryProject = (UecSalaryProject) arg.get("uecSalaryProject");
		PubUtil.fillBeanFromPo(uecSalaryProject, bean);
	}

	/**
	 * 保存.
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = checkUacStaffData();
		if (msg != null) {
			Messagebox.show(msg);
			return;
		}

		if (EnumUecSalary.ADD.getValue().equals(opType)) {
			uecSalaryProject = UecSalaryProject.newInstance();
			PubUtil.fillPoFromBean(bean, uecSalaryProject);

			uecSalaryProject.addOnly();
			Events.postEvent(Events.ON_OK, bean.getUecSalaryPrjEditWin(),
					uecSalaryProject);
		} else if (EnumUecSalary.MOD.getValue().equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecSalaryProject);
			uecSalaryProject.updateOnly();
			Events.postEvent(Events.ON_OK, bean.getUecSalaryPrjEditWin(),
					uecSalaryProject);
		}

		onCancel();
	}

	/**
	 * 取消.
	 */
	public void onCancel() {
		bean.getUecSalaryPrjEditWin().onClose();
	}

	private String checkUacStaffData() {
		// BaseInfo Check
		if (StrUtil.isNullOrEmpty(bean.getCode().getValue())) {
			return "请填写薪资项目编码";
		} else if (StrUtil.checkBlank(bean.getCode().getValue())) {
			return "薪资项目编码中有空格";
		} else if (StrUtil.isNullOrEmpty(bean.getName().getValue())) {
			return "请填写薪资项目名称";
		} else if (StrUtil.checkBlank(bean.getName().getValue())) {
			return "薪资项目名称中有空格";
		} else {
			return null;
		}
	}
}
